//
//  ConvertQuanViewController.m
//  1243
//
//  Created by 杨森 on 16/4/8.
//  Copyright © 2016年 1243. All rights reserved.
//

#import "ConvertQuanViewController.h"
#import "ConcertQuanTableViewCell.h"
#import "NetWorkTools.h"
#import "UIImageView+WebCache.h"
#import "ShoeBagDressViewController.h"
#import "ShoperDetailViewController.h"
@interface ConvertQuanViewController ()<UITableViewDelegate,UITableViewDataSource>

@property(nonatomic,strong)UITableView *convertTableView;

@property(nonatomic,strong)NSArray *GoArray;

@end

@implementation ConvertQuanViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    
    self.title = @"兑换";
    self.view.backgroundColor = [UIColor whiteColor];
    [self addUI];
    
    [self getShoesAndColorData];
}

- (void)addUI
{
    CGFloat h = 40;
    UIView *lastView = [[UIView alloc] initWithFrame:CGRectMake(0,HEIGHT-h, WEITH, h)];
    
    lastView.backgroundColor = [UIColor whiteColor];
    
    UIButton *outDateBtn = [UIButton buttonWithType:UIButtonTypeCustom];
    [lastView addSubview:outDateBtn];
    [outDateBtn setBackgroundImage:[UIImage imageNamed:@"查看兑换记录"] forState:UIControlStateNormal];
    
    CGFloat btnW = 100;
    outDateBtn.frame = CGRectMake(WEITH/2-btnW/2,10,btnW,15);
    
    self.convertTableView = [[UITableView alloc] initWithFrame:CGRectMake(0,0, WEITH, HEIGHT)];
    self.convertTableView.delegate = self;
    self.convertTableView.dataSource = self;
   
    [self.view addSubview:self.convertTableView];
    [self.view addSubview:lastView];
    self.convertTableView.rowHeight = 160;
    
    UIBarButtonItem *item = [[UIBarButtonItem alloc] initWithTitle:@"使用说明" style:UIBarButtonItemStylePlain target:self action:@selector(useItemClick)];
    self.navigationItem.rightBarButtonItem = item;
    
}

- (void)useItemClick
{
    NSLog(@"使用说明");
}



- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return  self.GoArray.count;
}


- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    static NSString *identifier= @"convertCell";
    
    ConcertQuanTableViewCell *cell=[tableView dequeueReusableCellWithIdentifier:identifier];
    
    if (cell==nil) {
       
        cell = [[[NSBundle mainBundle] loadNibNamed:@"ConcertQuanTableViewCell" owner:nil options:nil] lastObject];
        cell.selectionStyle = UITableViewCellSelectionStyleNone;
    }

    
    NSString *URLString = @"http://120.76.162.124:8083";
    
    NSString *PicString = [URLString stringByAppendingString:[self.GoArray[indexPath.row] objectForKey:@"PictureURL"]];
    
    [cell.shopImageView sd_setImageWithURL:[NSURL URLWithString:PicString]placeholderImage:[UIImage imageNamed:@"dress.png"]];
   
    NSString *string =[self.GoArray[indexPath.row] objectForKey:@"Title"];
    cell.shopName.text = string;
    
    NSString *pstring =[self.GoArray[indexPath.row] objectForKey:@"NeedCardNum"];
    cell.priceLabel.text = [NSString stringWithFormat:@"购物券 : %@ 张",pstring];

    [cell.tuiHuanButton addTarget:self action:@selector(tuiHuanButtonClick) forControlEvents:UIControlEventTouchUpInside];
    cell.tuiHuanButton.userInteractionEnabled = NO;
    return cell;
}

- (void)tuiHuanButtonClick
{
//     //  兑换方法
//    ShoperDetailViewController *a = [[ShoperDetailViewController alloc] init];
//    
//    a.xingYingShopID = [self.GoArray[indexPath.row] objectForKey:@"Id"];
//    a.str = @"兑换";
//    [self.navigationController pushViewController:a animated:YES];
//    
    
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    ShoperDetailViewController *a = [[ShoperDetailViewController alloc] init];
    
    a.xingYingShopID = [self.GoArray[indexPath.row] objectForKey:@"Id"];
    NSString *pstring =[self.GoArray[indexPath.row] objectForKey:@"NeedCardNum"];
    NSString *str = [NSString stringWithFormat:@"%@%@",@"兑换",pstring];
    a.str = @"兑换";
    [self.navigationController pushViewController:a animated:YES];
}
//获取给分类下的所有商品
- (void)getShoesAndColorData{
    
    NSDictionary *dict = @{
                           @"SortName":@"",
                           @"PageIndex":@"1",
                           @"PageSize":@"10",
                           @"Postion":@"2",
                    
                           //                           @"Sign":@"9568F4B2ED27BD2E3BE1A1C488669658A508B626"
                           };
    
    [[NetWorkTools sharedNetWorkTools] objectWithURLDict:dict AndMenthodName:@"PRODUCTLISTQUERY" completeBlock:^(id object) {
        
        NSString *DataString = [object objectForKey:@"BackData"];
        
        NSDictionary *dict = [NetWorkTools dictionaryWithJsonString:DataString];
        
        
        self.GoArray = [dict objectForKey:@"ds"];
        NSLog(@"%@",self.GoArray);
        [self.convertTableView reloadData];

    }];
    
}



@end
